package com.bkhc.product.repository;

import com.bkhc.common.vo.UserRoleVo;
import com.bkhc.product.entity.Brand;
import com.bkhc.product.entity.BrandProduct;
import com.bkhc.product.vo.BrandProductDetailVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Repository
public interface BrandProductRepository extends JpaRepository<BrandProduct,String>, JpaSpecificationExecutor<BrandProduct> {

    BrandProduct findByProductCodeAndBrandCode(String productCode, String brandCode);

    default Page<BrandProduct> findAll(BrandProduct brandProduct, Pageable pageable) {
        return findAll(new Specification<BrandProduct>() {
            @Override
            public Predicate toPredicate(Root<BrandProduct> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicate = new ArrayList<>();
                if (!StringUtils.isEmpty(brandProduct.getBrandCode())) {
                    predicate.add(criteriaBuilder.equal(root.get("brandCode").as(String.class), brandProduct.getBrandCode()));
                }
                if (!StringUtils.isEmpty(brandProduct.getBrandName())) {
                    predicate.add(criteriaBuilder.like(root.get("brandName").as(String.class), "%" + brandProduct.getBrandName() + "%"));
                }
                if (!StringUtils.isEmpty(brandProduct.getProductCode())) {
                    predicate.add(criteriaBuilder.equal(root.get("productCode").as(String.class), brandProduct.getProductCode()));
                }
                if (!StringUtils.isEmpty(brandProduct.getProductName())) {
                    predicate.add(criteriaBuilder.like(root.get("productName").as(String.class), "%" + brandProduct.getProductName() + "%"));
                }
                Predicate[] pre = new Predicate[predicate.size()];

                return criteriaQuery.where(predicate.toArray(pre)).getRestriction();
            }
        }, pageable);

    }

    @Query("select new com.bkhc.product.vo.BrandProductDetailVO(b.brandCode,b.brandName,b.productCode,b.productName,p.partnerCode,p.partnerName) from BrandProduct b left join PartnerBrand p on b.brandCode=p.brandCode and b.productCode=p.productCode where b.productCode=?1 and b.brandCode=?2")
    BrandProductDetailVO findBrandProdectDetail(String productCode, String brandCode);

    List<BrandProduct> findByBrandCode(String brandCode);

    Page<BrandProduct> findByProductCodeIn(List<String> productCodeList, Pageable pageRequest);


    void deleteByProductCode(String productCode);

    @Query("update BrandProduct b set b.productName=:productName where b.productCode=:productCode")
    @Modifying
    void updateProductName(@Param("productName") String productName,@Param("productCode") String productCode);
    @Query("select b from BrandProduct b where b.brandCode=:brandCode and b.productCode in (select c.productCode from Product c where c.partnerId=:userId) ")
    List<BrandProduct> findByBrandCodeAndUser(@Param("brandCode")String brandCode,@Param("userId") String userId);

    void deleteByBrandCodeAndProductCode(String brandCode, String productCode);
}
